package command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import impl_player.PlayerAcceptCommand;
import impl_player.PlayerAnswerCommand;
import impl_player.PlayerAskCommand;
import impl_player.PlayerExitCommand;
import impl_player.PlayerHelpCommand;
import impl_player.PlayerListCommand;
import impl_player.PlayerLoginCommand;
import impl_player.PlayerQuitCommand;
//import impl_player.PlayerQuitCommand;
import impl_player.PlayerScoreCommand;
import impl_player.PlayerSignupCommand;
import pojo.Player;

public class PlayerCommandFactory extends CommandFactory {

	private Player player;

	public PlayerCommandFactory(Player player) {
		this.player = player;
	}

	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.HELP, PlayerHelpCommand.class);
		commandMap.put(CommandCode.EXIT, PlayerExitCommand.class);
		commandMap.put(CommandCode.ASK, PlayerAskCommand.class);
		commandMap.put(CommandCode.ANSWER, PlayerAnswerCommand.class);
		commandMap.put(CommandCode.ACCEPT, PlayerAcceptCommand.class);
		commandMap.put(CommandCode.SCORE, PlayerScoreCommand.class);
		commandMap.put(CommandCode.LIST, PlayerListCommand.class);
		commandMap.put(CommandCode.LOGIN, PlayerLoginCommand.class);
		commandMap.put(CommandCode.SIGNUP, PlayerSignupCommand.class);
		commandMap.put(CommandCode.QUIT, PlayerQuitCommand.class);
	}

	public Command buildCommand(CommandCode cmd) {
		Class<? extends Command> clazz = commandMap.get(cmd);

		try {
			Constructor<? extends Command> c = clazz.getConstructor(Player.class);
			Command command = c.newInstance(player);
			return command;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
